package com.tuling.controller;

import com.tuling.entity.OrderInfo;
import com.tuling.entity.ProductInfo;
import com.tuling.mapper.OrderInfoMapper;
import com.tuling.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
public class OrderInfoController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private DiscoveryClient discoveryClient;


    @RequestMapping("/selectOrderInfoById/{orderNo}")
    public Object selectOrderInfoById(@PathVariable("orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoMapper.selectOrderInfoById(orderNo);
        if (null == orderInfo) return logMsg("根据 orderNo: " + orderNo + " 查询没有该订单");
        // 从nacos server获取 product-info的地址
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("product-center");
        if (null == serviceInstanceList || serviceInstanceList.isEmpty())
            return logMsg("用户微服务没有对应的实例可用");
        // 获取第0个元素
        String targetUri = serviceInstanceList.get(0).getUri().toString();
        ResponseEntity<ProductInfo> responseEntity = restTemplate.getForEntity(
                targetUri + "/selectProductInfoById/" + orderInfo.getProductNo(),
                ProductInfo.class
        );
        ProductInfo productInfo = responseEntity.getBody();
        if (productInfo == null) return logMsg("没有对应的商品");
        return convertOrderInfo(orderInfo, productInfo);
    }

    private OrderVo convertOrderInfo(OrderInfo orderInfo, ProductInfo productInfo) {
        OrderVo orderVo = new OrderVo();
        orderVo.setOrderNo(orderInfo.getOrderNo());
        orderVo.setUserName(orderInfo.getUserName());
        orderVo.setProductName(productInfo.getProductName());
        orderVo.setProductNum(orderInfo.getProductCount());
        return orderVo;
    }

    private Object logMsg(String msg) {
        String resultMsg = msg;
        log.info(resultMsg);
        return resultMsg;
    }

    @GetMapping("/getServiceList")
    public List<ServiceInstance> getServiceList() {
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("order-center");
        log.info(serviceInstanceList.toString());
        return serviceInstanceList;
    }
}
